<?php

class Emms_Form_Admin_Business extends HTech_Form
{
    protected $_primary  = 'BusinessId';
    
    private $_defaultClient;
    private $_defaultActivity;
    private $_typeOptions = array();
                    
    public function getValues()
    {
        $data = parent::getValues();
        unset($data['Client']);
        unset($data['Activity']);
        return $data;
    }
    
    private function _init()
    {
        if(!$this->_request->isPost()) {
            try {
                $businessId = (int) $this->_request->getParam('id');
                if($businessId) {
                    $businesses = new Businesses();
                    $this->_defaultClient = $businesses->getRecord($businessId)->d->ClientFullName;
                    $this->_defaultActivity = $businesses->getRecord($businessId)->d->Activity;
                } else {
                    $clients = new Clients();
                    $this->_defaultClient = $clients->getRecord((int) $this->_request->getParam('client'))->d->FullName;
                }
            } catch (Exception $e) {
                echo $e->getMessage(); exit;
            }
        } else {
            $this->_defaultActivity = $this->_request->getPost('Activity');
        }
        $this->_typeOptions = $this->_db->fetchPairs('select TypeId, Type from view_business_types where Activity = ?', $this->_defaultActivity);
    }
    
    public function __construct() 
    {
        parent::__construct();
        $this->_init();
        
        $this->setName('Business');
        
        $this->setAttrib('title', $this->translate("Business form"));
        $this->setDescription($this->translate("Edit business info or add a new one"));
        
        $businessId = new Zend_Form_Element_Hidden('BusinessId');
                
        $clientId = new Zend_Form_Element_Hidden('ClientId');
        $clientId->setValue((int) $this->_request->getParam('client'));
        
        $client = new Zend_Form_Element_Text('Client');
        $client->setLabel("Client")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $name = new Zend_Form_Element_Text('Name');
        $name->setLabel("Business name")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 3,
                    'max'      => 128,
                    'messages' => $this->translate("From %min% to %max% characters only")
                )); 
        
        $activity = new Zend_Form_Element_Select('Activity');
        $activity->setLabel("Activity")                
            ->setAttrib('size','4')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions(
                array(
                    'Service' => $this->translate("Service"), 
                    'Industry' => $this->translate("Industry"),
                    'Commerce' => $this->translate("Commerce"), 
                    'Farm' => $this->translate("Farm")));         
        
        $typeId = new Zend_Form_Element_Select('TypeId');
        $typeId->setLabel("Business type")                
            ->setAttrib('size','6')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions($this->_typeOptions); 
                        
        $govCode = new Zend_Form_Element_Text('GovCode');
        $govCode->setLabel("Business gov. code")
            ->addValidator('Regex', true, array(
                'pattern' => $this->_conf->regex->govCode->pattern,
                'messages' => sprintf($this->translate("Valid format: %s"), $this->_conf->regex->govCode->message)))
            ->addFilter('Null', array('type' => 'string'));  
        
        $description = new Zend_Form_Element_Textarea('Description');
        $description->setLabel("Description")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $status = new Zend_Form_Element_Select('Status');
        $status->setLabel("Status")                
            ->setAttrib('size', '2')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions(array('Active' => $this->translate("Active"), 'Inactive' => $this->translate("Inactive")));   

        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->addElements(array($client,$name,$govCode,$activity,$typeId,$status,$description,$submit,$businessId,$clientId));
        
        $this->setDefaults(array('Client' => $this->_defaultClient, 'Activity' => $this->_defaultActivity));
    }
}